package com.client.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.client.bean.BirdList;
import com.client.bean.BirdMessage;
import com.client.common.SpinnerProgressDialog;
import com.client.common.ValueStack;
import com.client.task.UnUploadBirdMessagesTask;
import com.clinet.cache.AsyncImageLoader;

public class UnunloadBirdAct extends BaseAct {

	private Context mContext = UnunloadBirdAct.this;
	public static AsyncImageLoader mAsyncImageLoader = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ununload_brid);
		initBottomBar();
		init();
		onclick();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mAsyncImageLoader.clearImageMap();
		mAsyncImageLoader.shutdownThreadPool();
	}
	UnUploadBirdMessagesTask.Callback callback = new UnUploadBirdMessagesTask.Callback() {
		@Override
		public void onComplete(final Integer result) {

			SpinnerProgressDialog.dismissDialog();
			switch (result) {
			case 1:
				initListView();

				break;
			case 0:

				break;
			}
		}
	};
	private void getData() {
		SpinnerProgressDialog.showDialog(this,"正在获取数据,请稍后...");
		mUnUploadBirdMessagesTask = new UnUploadBirdMessagesTask(mContext, callback);
		mUnUploadBirdMessagesTask.execute(mBirdList);
	}
	private UnUploadBirdMessagesTask mUnUploadBirdMessagesTask = null;

	private void onclick() {
		allupload.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

			}

		});

	}

	private BirdList mBirdList = new BirdList();

	private ImageView allupload = null;
	private ListView listview = null;
	private LayoutInflater layoutFlater = null;

	private void init() {
		layoutFlater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mAsyncImageLoader = new AsyncImageLoader();
		listview = (ListView) findViewById(R.id.listview);
		allupload = (ImageView) findViewById(R.id.allupload);
	}// -----------------------------列表----------------------S

	public void initListView() {
		if (mBirdList == null || mBirdList.getTotalNum() <= 0) {
			return;
		}
		listview = (ListView) findViewById(R.id.listview);
		listview.setDivider(null);
		mListViewAdapter = new TimeListAdapter();
		listview.setAdapter(mListViewAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

			}
		});

	}

	private TimeListAdapter mListViewAdapter = null;

	public class TimeListAdapter extends BaseAdapter {

		public TimeListAdapter() {
			super();

		}

		public int getCount() {
			return mBirdList.getTotalNum();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertview, ViewGroup parent) {
			final ViewHolderListViewItem holder;
			if (convertview == null) {
				convertview = layoutFlater.inflate(R.layout.unupload_list_item,
						null);
				holder = new ViewHolderListViewItem();
				holder.birdImage = (ImageView) convertview
						.findViewById(R.id.birdImage);
				holder.date = (TextView) convertview.findViewById(R.id.date);
				holder.birdName = (TextView) convertview
						.findViewById(R.id.birdName);
				holder.num = (TextView) convertview.findViewById(R.id.num);
				holder.kind = (TextView) convertview.findViewById(R.id.kind);
				holder.healthNum = (TextView) convertview
						.findViewById(R.id.healthNum);
				holder.sickNum = (TextView) convertview
						.findViewById(R.id.sickNum);
				holder.deadNum = (TextView) convertview
						.findViewById(R.id.deadNum);
				holder.edit = (ImageView) convertview.findViewById(R.id.edit);
				holder.line = (ImageView) convertview.findViewById(R.id.line);
				convertview.setTag(holder);
			} else {
				holder = (ViewHolderListViewItem) convertview.getTag();
			}

			final BirdMessage bean = mBirdList.getItems().get(position);
			holder.birdName.setText(bean.birdName);
			holder.date.setText(bean.date);
			holder.birdName.setText(bean.birdName);
			holder.num.setText(bean.num);
			holder.kind.setText(bean.kind);
			holder.healthNum.setText(bean.healthNum);
			holder.sickNum.setText(bean.sickNum);
			holder.deadNum.setText(bean.deadNum);

			holder.edit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					DetaiAct.MODE = DetaiAct.EDITMODE;
					ValueStack.getInstance().setValue("bean", bean);
					Intent intent = new Intent(mContext, DetaiAct.class);
					startActivity(intent);
				}
			});
			String url = bean.birdImage;
			holder.birdImage.setTag(url);
			// 载入图片
			UnunloadBirdAct.mAsyncImageLoader.getListViewLogoItem(mContext,
					bean.birdImage, holder.birdImage, listview);
			return convertview;
		}
	}

	private static class ViewHolderListViewItem {
		public ImageView birdImage = null;
		public TextView date = null;
		public TextView birdName = null;
		public TextView num = null;
		public TextView kind = null;
		public TextView healthNum = null;
		public TextView sickNum = null;
		public TextView deadNum = null;
		public ImageView edit = null;
		public ImageView line = null;
	}

	// -----------------------------网格----------------------E
}